import Vue from 'vue'
import VueRouter from 'vue-router'
import { Message } from 'element-ui'

Vue.use(VueRouter)

const routes = [
  {
    path: '/',
    name: '',
    component: () => import(/* webpackChunkName: "about" */ '../views/login.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import(/* webpackChunkName: "about" */ '../views/login')
  },
  {
    path: '/equipment',
    name: 'equipment',
    component: () => import(/* webpackChunkName: "about" */ '../views/equipment.vue')
  }
]

const router = new VueRouter({
  routes
})

// 在路由配置文件中注册一个全局前置守卫(router.beforeEach)函数

router.beforeEach(( to , from , next )=>{
  // to : 将要跳转的目标路由
  // from : 当前正要跳转的路由
  // next ： function：一定要调用该方法来resolve这个钩子，执行结果依赖于next的参数
  if(localStorage.getItem('realName')){
    next()
  }else{
    if (to.path === '/login') {
      next()
    } else {
      console.error('未获取到token信息，请重新登录')
      Message.error('未获取到token信息，请重新登录')
      next({path:'/login'})
    }
  }
})

export default router
